<template>
	<view>
		<uni-nav-bar dark :fixed="true" shadow background-color="#00aaff" status-bar left-icon="left" title="修改密码" @clickLeft="back"/>
		<view class="body">
			<uni-easyinput style="margin-top: 40rpx;" prefixIcon="locked" type="text" v-model="formData.old_password" clearable placeholder="请输入原密码" />
			<uni-easyinput style="margin-top: 40rpx;" prefixIcon="locked-filled" type="password" v-model="formData.new_password" placeholder="请输入新密码" />
			<uni-easyinput style="margin-top: 40rpx;" prefixIcon="locked-filled" type="password" v-model="formData.new_2_password" placeholder="请重复输入新密码" />
		</view>
		<button style="margin: 60rpx; background-color: #ff8d02;color: #fff;" type="default" @click="submit" :loading="loading">提交</button>
		<view class="bottom">
			Copyright © 2022 乐易通付 浙ICP备2022025523号-1
		</view>
	</view>
</template>
 
<script>
	export default {
		name: 'login',
		data() {
			return {
				// 表单数据
				formData: {
					old_password:'',
					new_password: '',
					new_2_password: ''
				},
				loading: false
			};
		},
		
		
		methods: {
			back() {
				uni.redirectTo({
					url: '/pages/index/index'
				})
			},
			
			
			submit() {
				if(this.loading) {
					return
				}
				
				if(this.formData.old_password == "") {
					uni.showToast({
						icon:'error',
						title: '请输入原登录密码'
					})
					return ;
				}
				
				if(this.formData.new_password == "") {
					uni.showToast({
						icon:'error',
						title: '请输入新登录密码'
					})
					return ;
				}
				
				if(this.formData.new_password !== this.formData.new_2_password) {
					uni.showToast({
						icon:'error',
						title: '两次密码不同'
					})
					return ;
				}
				
				uni.showLoading({
					title: '提交中...'
				})
				
				this.loading = true
				
				let data = {
					uid: uni.getStorageSync('uid'),
					...this.formData
				}
				
				uni.request({
					url: '/login/updatePassword',
					data,
					method: "POST",
					success: (res) => {
						if(res.data.success) {
							uni.clearStorage();
							
							uni.showToast({
								icon: 'success',
								title: '修改成功'
							})
							
							setTimeout(() => {
							 uni.redirectTo({
								url: '/pages/login/index'
							 })
							})
						} else {
							uni.showToast({
								icon:'error',
								title: res.data.errMessage
							})
						}
					},
					complete: () => {
						uni.hideLoading()
						this.loading = false
					}
				})
			}
		}
	}
</script>
 
<style lang="scss" scoped>
	.page {
		width: 750rpx;
		background: #fff;
		padding: 60rpx 40rpx;
		box-sizing: border-box;
	}
	
	.body {
		margin-top: 180rpx;
		height: 300rpx;
		padding: 60rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-around;
	}

	.bottom {
		position: fixed;
		bottom: 20rpx;
		width: 100%;
		text-align: center;
		left: 0;
		font-size: 24rpx;
		color: #fff;
	}
</style>